﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Whoever.Framework;
using System.Reflection;

namespace Whoever.Framework.Attributes
{
    [AttributeUsage(AttributeTargets.Property)]
    public class SelectableRelationship : Attribute
    {
        public SelectableRelationship(string propertyName = null) { string dato = ""; }

        public SelectableRelationship(Type entityWorkingType,Type entityType, Type managerType)
        {
            EntityName = entityType.Name;

            var properties = entityWorkingType.GetProperties().Where(p => p.PropertyType.Name == entityType.Name);
            var countProperty = properties.Count();
            if (countProperty == 0)
                throw new Exception(string.Format("No existe ninguna propiedad del tipo {0} en la clase {1}", entityType.Name, entityWorkingType.Name));

            if (countProperty > 1)
                throw new Exception(string.Format("Existe mas de una propiedad del tipo {0} en la clase {1}", entityType.Name, entityWorkingType.Name));

            var currentProperty = properties.First();

            ForeignKeyName = currentProperty.Name;
            RelationValueName = ((PropertyInfo)Reflection.Helpers.GetPropertyWithAttribute(entityType, typeof(IdentityField))).Name;
            


            //ForeignKeyName = "CategoryId", RelationKeyName = "Id", RelationValueName = "Name", IsRequired = true

            //Reflection.Helpers.GetAttributes(
        }

        public string EntityName { get; set; }

        public string RelationValueName { get; set; }

        public string RelationTextName { get; set; }

        public string ForeignKeyName { get; set; }

        public bool IsRequired { get; set; }

        public Type EntityType { get; set; }

        public Type ManagerType { get; set; }
    }
}
